package cn.edu.hit.education.action;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import cn.edu.hit.education.dialog.UniversityDialog;
import cn.edu.hit.education.pojo.University;
import cn.edu.hit.education.service.IUniversityService;
import cn.edu.hit.education.view.DepartmentExploreView;
import educationschedule.Activator;
import educationschedule.Application;

public class UpdateUniversityAction extends Action {
	private DepartmentExploreView viewPart;
	IUniversityService universityService = (IUniversityService)Application.applicationContext.getBean("universityServiceImpl");
	private int unversityId;
	
	public UpdateUniversityAction(DepartmentExploreView viewPart,int unversityId){
		this.setImageDescriptor(AbstractUIPlugin.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/toolbar/course_code_16px.png"));
		this.setToolTipText("修改学校");
		this.setText("修改学校");
		this.viewPart = viewPart;
		this.unversityId = unversityId;
	}
	
	@Override
	public void run() {
		Shell parentShell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
		UniversityDialog universityDialog = new UniversityDialog(parentShell);
		University university = universityService.queryUniversityByPrimaryKey(unversityId);
		universityDialog.setUniversity(university);
		if(IDialogConstants.OK_ID == universityDialog.open()){
			int count = universityService.update(university);
			if(count > 0){
				viewPart.refreshUpdateData(university);
			}
		}
		super.run();
	}

	

}
